function MainAssistant(argFromPusher) {}

MainAssistant.prototype = {
	setup: function() {
		Ares.setupSceneAssistant(this);

		usernameCookie = new Mojo.Model.Cookie("username_cookie");
		passwordCookie = new Mojo.Model.Cookie("password_cookie");
		authCode = null;
		blogListXML = null;
		activeBlogID = null;
		activeBlogTitle = null;
		
		this.controller.setupWidget(Mojo.Menu.viewMenu,
			{
				spacerHeight: 45//,
				//menuClass:'no-fade'
			},
			{
				visible: true,
				items: [
					{items: [
						{label: "Blogs", width: Mojo.Environment.DeviceInfo.screenWidth}
					]}
				]
			}
		);
		
		
		this.mainSpinner = this.controller.get("mainSpinner");
		this.controller.setupWidget("mainSpinner", {'spinnerSize':Mojo.Widget.spinnerLarge}, this.spinnerModel = {'spinning': true});
		this.mainSpinnerScrim = Mojo.View.createScrim(this.controller.document, {scrimClass: 'palm-scrim'});
		this.mainSpinnerScrim.hide();
		this.controller.get("mainScrimContainer").appendChild(this.mainSpinnerScrim).appendChild(this.mainSpinner);
		
		this.tappedBlogListEventListener = this.tappedBlogList.bindAsEventListener(this);
		this.controller.listen('blogList', Mojo.Event.listTap, this.tappedBlogListEventListener);
	},

	activate: function() {
		var usernameString = usernameCookie.get();
		var passwordString = passwordCookie.get();
		
		this.mainSpinnerScrim.show();
		
		if (usernameString && passwordString && !authCode) {
			var authRequest = new Ajax.Request("https://www.google.com/accounts/ClientLogin", {
				method: 'post',
				postBody: 'Email=' + encodeURIComponent(usernameString) + '&Passwd=' + encodeURIComponent(passwordString) + '&service=blogger' + '&accountType=GOOGLe' + '&source=gworley3-plogger-1',
				onSuccess: this.authRequestSuccess.bind(this),
				onFailure: this.authRequestFailure.bind(this)
			});
		}
		else if (!authCode)
		{
			this.controller.stageController.pushScene("prefs");
		}
		else {
			this.mainSpinnerScrim.hide();
		}
	},
/*
	handleCommand: function(event) {
		switch (event.type) {
			// enable default items here.
		case Mojo.Event.commandEnable:
			switch (event.command) {
			case Mojo.Menu.prefsCmd:
				event.stopPropagation();
				break;
			}
			break;

			// handle items here.
		case Mojo.Event.command:
			switch (event.command) {
			case Mojo.Menu.prefsCmd:
				this.controller.stageController.pushScene("prefs");
				break;
			}
			break;
		}
	},*/
	cleanup: function() {
		this.controller.stopListening('blogList', Mojo.Event.listTap, this.tappedBlogListEventListener);
		Ares.cleanupSceneAssistant(this);
	},

	authRequestSuccess: function(authResponse) {
		authCode = authResponse.responseText.split('=').pop().slice(0, -1);
		var blogListRequest = new Ajax.Request("http://www.blogger.com/feeds/default/blogs", {
			method: 'get',
			requestHeaders: {
				Authorization: 'GoogleLogin auth=' + authCode
			},
			onSuccess: this.blogListRequestSuccess.bind(this),
			onFailure: this.blogListRequestFailure.bind(this)
		});
	},

	authRequestFailure: function(authResponse) {
		Mojo.Log.info(authResponse.responseText);
		authCode = null;
		
		if (authResponse.responseText.match(/CaptchaRequired/))
		{
			this.controller.showAlertDialog(
			{
				onChoose: function(value)
				{
					var openURL = new Mojo.Service.Request
					(
						'palm://com.palm.applicationManager',
						{
							method: 'open',
							parameters:
							{
								id: 'com.palm.app.browser',
								params: {target: 'https://www.google.com/accounts/DisplayUnlockCaptcha?service=blogger'}
							}
						}
					);
				},
				title: "CAPTCHA Required",
				message: "Google is asking you to complete a CAPTCHA before you can login.  Tap OK to launch the web browser to complete request.",
				preventCancel: true,
				choices: [{label: "OK", value: "OK", type: "dismiss"}]
			});
		}
		else 
		{
			this.controller.showAlertDialog(
			{
				onChoose: function(value) {this.controller.stageController.pushScene("prefs");},
				title: "Error",
				message: "Authentication to Blogger failed.  Check your username and password.",
				preventCancel: true,
				choices: [{label: "OK", value: "OK", type: "dismiss"}]
			});
		}
		this.mainSpinnerScrim.hide();
	},

	blogListRequestSuccess: function(blogListResponse) {
		blogListXML = blogListResponse.responseXML;
		var blogEntryList = blogListXML.getElementsByTagName("entry");
		//Mojo.Log.info(this.$.blogList.items[0].label);
		//Mojo.Log.info(blogListModel.items);
		this.blogListModel.items = [];
		for (var blogEntryIndex = 0; blogEntryIndex < blogEntryList.length; blogEntryIndex++) {
			//Mojo.Log.info(blogEntryList[blogEntryIndex].getElementsByTagName("id")[0].childNodes[0].nodeValue);
			this.blogListModel.items.push({
				label: blogEntryList[blogEntryIndex].getElementsByTagName("title")[0].childNodes[0].nodeValue,
				id: blogEntryList[blogEntryIndex].getElementsByTagName("id")[0].childNodes[0].nodeValue
			});
		}
		
		this.blogListModel.items.push({});
		this.controller.modelChanged(this.blogListModel);
		this.mainSpinnerScrim.hide();
	},

	blogListRequestFailure: function(blogListResponse) {
		this.controller.errorDialog("Failed to load the blog list.");
		Mojo.Log.info("Failed to get the blog list:  " + blogListResponse.getAllHeaders());
		Mojo.Log.info(blogListResponse.responseText);
		this.mainSpinnerScrim.hide();
	},

	tappedBlogList: function(event) {
		activeBlogID = event.item.id.split('.').pop().split('-').pop();
		activeBlogTitle = event.item.label;
		Mojo.Log.info(event.item.id);
		Mojo.Log.info(activeBlogID);
		this.controller.stageController.pushScene('post-list');
	}
};